﻿using AutoMapper;
using Common.Helper;
using DbModel;
using DbModel.Models;
using DbModel.ViewModel.Article;
using IServices;
using IServices.Base;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;


namespace WebApi.Controllers
{
    /// <summary>
    /// 文章控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ArticleController : ControllerBase
    {
        private readonly IArticleService _articleService;
        private readonly IBaseServices<UserInfo> _userInfoService;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="articleService"></param>
        /// <param name="userInfoService"></param>
        /// <param name="mapper"></param>
        public ArticleController(IArticleService articleService, IBaseServices<UserInfo> userInfoService, IMapper mapper)
        {
            _articleService = articleService;
            _userInfoService = userInfoService;
            _mapper = mapper;
        }
        /// <summary>
        /// 分页获取文章列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<List<ArticleDto>>> GetArticleList(int page, int pageSize)
        {
            var entityList = await _articleService.GetPagedListAsync(page, pageSize, nameof(Article.CreateTime));
            var articleUserIdList = entityList.Select(x => x.CreateUserId);
            var userList = await _userInfoService.GetListAsync(x => articleUserIdList.Contains(x.Id));
            var response = _mapper.Map<List<ArticleDto>>(entityList);
            foreach (var item in response)
            {
                var user = userList.FirstOrDefault(x => x.Id == item.CreateUserId);
                item.UserName = user.UserName;
                item.HeadPortrait = user.HeadPortrait;
            }
            return new MessageModel<List<ArticleDto>>()
            {
                Success = true,
                Msg = "获取成功",
                Response = response
            };

        }
        /// <summary>
        /// 根据文章id获取文章详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<ArticleDetailsDto>> GetArticleDetail(int id)
        {
            var entity = await _articleService.GetArticleDetailAsync(id);
            var result = _mapper.Map<ArticleDetailsDto>(entity);
            return new MessageModel<ArticleDetailsDto>()
            {
                Success = true,
                Msg = "获取成功",
                Response = result
            };
        }
        /// <summary>
        /// 创建文章
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> CreateAsync(CreateArticleInputDto input)
        {
            var token = JwtHelper.ParsingJwtToken(HttpContext);
            var entity = _mapper.Map<Article>(input);
            entity.CreateTime = DateTime.Now;
            entity.CreateUserId = token.Uid;
            await _articleService.InsertAsync(entity, true);
            return new MessageModel<string>()
            {
                Success = true,
                Msg = "创建成功"
            };
        }

        /// <summary>
        /// 修改文章
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<MessageModel<string>> UpdateAsync(int id,UpdateArticleInputDto input)
        {
            var entity = await _articleService.GetByIdAsync(id);
            entity = _mapper.Map(input,entity);
            await _articleService.UpdateAsync(entity,true);
            return new MessageModel<string>()
            {
                Success = true,
                Msg = "更新成功",
            };
        }
        /// <summary>
        /// 删除文章
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<MessageModel<string>> DeleteAsync(int id)
        {
            var entity = await _articleService.GetAsync(i => i.Id == id);
            await _articleService.DeleteAsync(entity);
            return new MessageModel<string>()
            {
                Success = true,
                Msg = "删除成功"
            };
        }

        /// <summary>
        /// 收藏文章
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("{id}",Name ="CreateCollection")]
        public async Task<MessageModel<string>> CreateCollectionAsync(int id)
        {
            var token = JwtHelper.ParsingJwtToken(HttpContext);
            await _articleService.AddArticleCollection(id, token.Uid);
            return new MessageModel<string>()
            {
                Success = true,
                Msg = "收藏成功"
            };
        }
        /// <summary>
        /// 添加文章评论
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost(Name ="CreateArticleComments")]
        public async Task<MessageModel<string>> CreateArticleCommentsAsync(int id,CreateArticleCommentsInputDto input)
        { 
            var token = JwtHelper.ParsingJwtToken(HttpContext);
            await _articleService.AddArticleComments(id,token.Uid,input.Content);
            return new MessageModel<string>()
            {
                Success = true,
                Msg = "评论成功"
            };
        }
        /// <summary>
        /// 删除文章评论
        /// </summary>
        /// <param name="articleId"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete(Name ="DeleteArticleComments")]
        public async Task<MessageModel<string>> DeleteArticleComments(int articleId,int id)
        {
            var entity = await _articleService.GetByIdAsync(articleId);
            entity.ArticleComments.Remove(entity.ArticleComments.FirstOrDefault(x=>x.Id==id));
            await _articleService.UpdateAsync(entity, true);
            return new MessageModel<string>()
            {
                Success = true,
                Msg = "删除评论成功"
            };
        }
    }
}
